// @ts-check

/* spell-checker: disable */
"use strict";

// -- https://gist.github.com/ScriptedAlchemy/642b31c375843152b7c7860ba47eaa7c#file-plugin-js
const path = require("node:path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { BannerPlugin, DefinePlugin, container } = require("webpack");

// const deps = require("../../package.json").dependencies;
const { InsertSnippetPlugin } = require("./plugin/rtModule.cjs");

/**
 * @type {import('webpack').Configuration}
 */
module.exports = {
  context: path.resolve(__dirname),
  entry: path.resolve(__dirname, "./index.ts"),
  mode: process.env.NODE_ENV || "development",
  devtool: "source-map",
  optimization: {
    minimize: false,
  },
  devServer: {
    // static: {
    //   directory: path.join(__dirname, "dist"),
    // },
    port: 7352,
    hot: true,
    liveReload: false,
  },
  resolve: {
    extensions: ["...", ".tsx"],
  },
  output: {
    // scriptType: "module",
    // publicPath: path.resolve(__dirname, "../public"),
  },
  module: {
    rules: [
      {
        test: /\.(m?[jt]sx?)$/,
        oneOf: [
          {
            test: /\.m?[jt]s$/,
            type: "javascript/auto",
            resolve: {
              // 要求完整拓展名
              // false会自动尝试添加扩展名
              fullySpecified: false,
            },
          },
          {
            test: /\.tsx$/,
            loader: "babel-loader",
            exclude: /node_modules/,
            options: {
              presets: ["babel-preset-solid", "@babel/preset-typescript"],
              plugins: [
                [
                  "solid-refresh/babel",
                  {
                    bundler: "webpack5", // or "rspack-esm"
                  },
                ],
              ],
            },
          },
        ],
      },
    ],
  },
  // externals: ["solid-js" ,"solid-js/web"],
  plugins: [
    new DefinePlugin({
      HYBRID: process.env.HYBRID === "1",
    }),
    // new Testing123Plugin(),
    new container.ModuleFederationPlugin({
      name: "appSolid",
      filename: "appSolidRemoteEntry.js",
      // source provider
      exposes: {
        "./app-main": "./App",
      },
      shared: {
        "solid-js": { singleton: true },
      },
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "./index.html"),
      // app2RemoteEntry: getRemoteEntryUrl(3002),
    }),
    // new BannerPlugin({
    //   banner: "// @ts-nocheck\n/* eslint-disable */\n",
    //   raw: true,
    //   entryOnly: true,
    // }),
  ],
};

// /**
//  * @param {number | string} port
//  * @returns {string}
//  */
// function getRemoteEntryUrl(port) {
//   const { CODESANDBOX_SSE, HOSTNAME = "" } = process.env;

//   // Check if the example is running on codesandbox
//   // https://codesandbox.io/docs/environment
//   if (!CODESANDBOX_SSE) {
//     return `//localhost:${port}/remoteEntry.js`;
//   }

//   const parts = HOSTNAME.split("-");
//   const codesandboxId = parts[parts.length - 1];

//   return `//${codesandboxId}-${port}.sse.codesandbox.io/remoteEntry.js`;
// }
